package com.kelvem.saas.workbanch.ontology.address.map.baidu;

import com.alibaba.fastjson.JSONObject;
import com.kelvem.saas.workbanch.core.utils.HttpClientUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class BaiduMapApi {
    
    public static RouteInfo calRouteInfo(BigDecimal srcLat, BigDecimal srcLng, BigDecimal dstLat, BigDecimal dstLng) {
    
        List<NameValuePair> nameValuePairList = new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("origin", srcLat.setScale(6, RoundingMode.HALF_UP).toString() + "," + srcLng.setScale(6, RoundingMode.HALF_UP).toString()));
        nameValuePairList.add(new BasicNameValuePair("destination", dstLat.setScale(6, RoundingMode.HALF_UP).toString() + "," + dstLng.setScale(6, RoundingMode.HALF_UP).toString()));
        nameValuePairList.add(new BasicNameValuePair("ak", "77790b03dfd9f6c50e00d49c91a7bdab"));
        JSONObject json = HttpClientUtil.get("http://api.map.baidu.com/directionlite/v1/driving", nameValuePairList);
    
        if (json.getJSONObject("result") == null) {
            return null;
        }
        if (json.getJSONObject("result").getJSONArray("routes") == null) {
            return null;
        }
        if (json.getJSONObject("result").getJSONArray("routes").size() <= 0) {
            return null;
        }
        
        JSONObject routeJson = json.getJSONObject("result").getJSONArray("routes").getJSONObject(0);
        RouteInfo route = routeJson.toJavaObject(RouteInfo.class);
        
        return route;
    }
    
    public static PlaceInfo calPlaceInfo(String query, String city) {
        
        List<NameValuePair> nameValuePairList = new ArrayList<>();
        nameValuePairList.add(new BasicNameValuePair("query", query));
        nameValuePairList.add(new BasicNameValuePair("region", city));
        nameValuePairList.add(new BasicNameValuePair("output", "json"));
        nameValuePairList.add(new BasicNameValuePair("city_limit", "false"));
        nameValuePairList.add(new BasicNameValuePair("ak", "77790b03dfd9f6c50e00d49c91a7bdab"));
        JSONObject json = HttpClientUtil.get("http://api.map.baidu.com/place/v2/suggestion", nameValuePairList);
        
        if (json.getJSONArray("result") == null) {
            return null;
        }
        if (json.getJSONArray("result").size() <= 0) {
            return null;
        }
        
        JSONObject placeJson = json.getJSONArray("result").getJSONObject(0);
        PlaceInfo place = placeJson.toJavaObject(PlaceInfo.class);
        if (placeJson.getJSONObject("location") != null) {
            place.setLat(placeJson.getJSONObject("location").getBigDecimal("lat"));
            place.setLng(placeJson.getJSONObject("location").getBigDecimal("lng"));
        }
        
        return place;
    }
    
    public static void main(String[] args) {
//        RouteInfo route = calRouteInfo(new BigDecimal(116.465851), new BigDecimal(39.913496), new BigDecimal(116.408133), new BigDecimal(40.052541));
//        System.out.println(route);
    
//        PlaceInfo place = calPlaceInfo("山东省枣庄市临沂市", "枣庄市");
        PlaceInfo place = calPlaceInfo("北京市市辖区昌平区", "市辖区");
        System.out.println(place);
    }
}

